package com.xyb.service;

import com.xyb.dao.CourseDocDao;
import com.xyb.entity.CourseDoc;
import com.xyb.utils.IdUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CourseDocService {
    @Resource
    private IdUtils idUtils;
    @Resource
    private CourseDocDao courseDocDao;

    public String insertCourseDocByCourseDetailId(Long courseDetailId, List<CourseDoc> courseDocList) {
        Integer column = courseDocDao.deleteByCourseDetailIdAndStatus(courseDetailId,1);
        for (int i = 0, size = courseDocList.size(); i < size; i++) {
            CourseDoc courseDoc = courseDocList.get(i);
            long nextId = idUtils.nextId();
            courseDoc.setCourseDocId(nextId);
            courseDoc.setCourseDetailId(courseDetailId);
            courseDoc.setCourseDocName("".equals(courseDoc.getCourseDocName().trim())?"资料":courseDoc.getCourseDocName().trim());
            courseDoc.setStatus(1);
            courseDoc.setShowOrder(i);
        }
        courseDocDao.saveAll(courseDocList);
        return "success";
    }

    public List<CourseDoc> selectCourseDocByCourseDetailId(Long courseDetailId) {
        List<CourseDoc> list = courseDocDao.findByCourseDetailIdAndStatusOrderByShowOrder(courseDetailId,1);
        return list;
    }

    public String deleteCourseDocByCourseDocId(Long courseDocId) {
        courseDocDao.deleteById(courseDocId);
        return "success";
    }

    public String updateCourseDocShowOrder(Long[] courseDocIds) {
        List<CourseDoc> list = new ArrayList<>();
        for (int i = 0, length = courseDocIds.length; i < length; i++) {
            CourseDoc courseDoc = new CourseDoc();
            courseDoc.setCourseDocId(courseDocIds[i]);
            courseDoc.setShowOrder(i);
            list.add(courseDoc);
        }
        courseDocDao.saveAll(list);
        return "success";
    }
}
